﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;

namespace Evolution.UI.WebParts
{
    [Serializable]
    public class HidePageElementsAction : BaseAction
    {
        #region Fields

        private readonly string[,] HideActions = 
        { 
            { "Site Actions menu", "Specifies that the Site Actions menu will be hidden." }, 
            { "Quick Launch menu", "Specifies that the Quick Launch menu will be hidden." }, 
            { "Recycle Bin", "Specifies that the Recycle Bin icon will be hidden." }, 
            { "View All Site Content", "Specifies that the \"View All Site Content\" link will be hidden." }, 
            { "Create Page item", "Specifies that the \"Create Page\" menu item will be hidden." },
            { "Edit Page item", "Specifies that the \"Edit Page\" menu item will be hidden." }, 
            { "Global navigation breadcrumb", "Specifies that the global navigation area (Welcome, My Site, ...) will be hidden." }, 
            { "Search box area", "Specifies that the search area will be hidden." }, 
            { "Main breadcrumb", "Specifies that the breadcrumb will be hidden." }
        };

        #endregion

        #region Constructors

        public HidePageElementsAction()
        {

        }

        public HidePageElementsAction(PageElements pageElements)
        {
            this.PageElements = pageElements;
        }

        #endregion

        #region Properties

        public PageElements PageElements { get; set; }

        public override string Caption
        {
            get { return "Hide page elements"; }
        }

        #endregion

        #region Methods

        public override void CreateInterface(ControlCollectionBuilder builder, PageActionEditor editor)
        {
            builder.AddStaticText(@"<div class=""UserControlBody"" style=""margin-bottom: 5px; margin-top: 5px"">Select the page elements to hide.</div>");
            for (int j = 0; j < HideActions.Length / 2; j++)
            {
                CheckBox check = new CheckBox();
                check.ID = "Evolution_Pawp_CheckHideActions" + (j + 1);
                check.Text = HideActions[j, 0];
                check.ToolTip = HideActions[j, 1];

                builder.AddControl(check);

                builder.AddStaticText("<br/>");
            }

            builder.AddErrorPlaceholder("Hide");
        }

        public override void Execute(Page page, PageActionWebPart part)
        {
            ContentPlaceHolder topNavBar = part.Page.Master.FindControl("PlaceHolderTopNavBar") as ContentPlaceHolder;
            SiteActions sa = topNavBar.FindControl("SiteActionsMenuMain") as SiteActions;

            if ((this.PageElements & PageElements.SiteActions) == PageElements.SiteActions)
                sa.Visible = false;
            if ((this.PageElements & PageElements.GlobalNavigation) == PageElements.GlobalNavigation)
            {
                ContentPlaceHolder globalNav = part.Page.Master.FindControl("PlaceHolderGlobalNavigation") as ContentPlaceHolder;
                if (globalNav != null)
                    globalNav.Controls.Clear();
            }
            if ((this.PageElements & PageElements.SearchArea) == PageElements.SearchArea)
            {
                ContentPlaceHolder searchArea = part.Page.Master.FindControl("PlaceHolderSearchArea") as ContentPlaceHolder;
                if (searchArea != null)
                    searchArea.Controls.Clear();
            }
            if ((this.PageElements & PageElements.TitleBreadcrumb) == PageElements.TitleBreadcrumb)
            {
                ContentPlaceHolder breadcrumb = part.Page.Master.FindControl("PlaceHolderTitleBreadcrumb") as ContentPlaceHolder;
                if (breadcrumb != null)
                    breadcrumb.Controls.Clear();
            }

            part.Page.ClientScript.RegisterStartupScript(
                this.GetType(),
                part.ID + "HidePageJSScript",
                String.Format(@"                    
                    Evolution_Pawp_HidePageElements(""{0}"", {1}, ""{2}"", true);",
                                                                         sa.ClientID,
                                                                         (int)this.PageElements,
                                                                         (new ArgumentOutOfRangeException()).Message),
                true);            
        }

        public override bool IsValidAction(PageActionWebPart part, out string errorMessage)
        {
            errorMessage = String.Empty;
            if (this.PageElements == PageElements.None)
                errorMessage = "No page element specified";

            return String.IsNullOrEmpty(errorMessage);
        }

        public override string GetDescription()
        {
            return "Hide page elements";
        }

        public override void ApplyChanges(PageActionEditor editor)
        {
            PageElements pageElements = PageElements.None;
            for (int j = 0; j < HideActions.Length / 2; j++)
            {
                CheckBox check = editor.FindControl("Evolution_Pawp_CheckHideActions" + (j + 1)) as CheckBox;
                if (check.Checked)
                    pageElements |= (PageElements)Math.Pow(2, j);
            }
            if (pageElements == PageElements.None)
                editor.RaiseError("Hide", "<br/>Specify at least one page element to hide.", String.Format("The action '{0}' requires the indication of at least one page element to hide.", this.Caption));

            this.PageElements = pageElements;
        }

        public override void SyncChanges(PageActionEditor editor)
        {
            PageElements pageElements = this.PageElements;
            for (int j = 0; j < HideActions.Length / 2; j++)
            {
                CheckBox check = editor.FindControl("Evolution_Pawp_CheckHideActions" + (j + 1)) as CheckBox;
                PageElements el = (PageElements)Math.Pow(2, j);
                if ((pageElements & el) == el)
                    check.Checked = true;
            }
        }

        #endregion
    }

    #region PageElements enum

    [Flags]
    public enum PageElements
    {
        None = 0,
        SiteActions = 1,
        QuickLaunch = 2,
        RecycleBin = 4,
        ViewAllSiteContent = 8,
        CreatePage = 16,
        EditPage = 32,
        GlobalNavigation = 64,
        SearchArea = 128,
        TitleBreadcrumb = 256
    }

    #endregion
}
